package com.yuandian.customer.perception.controller;

import com.yuandian.api.customer_perception.po.HighFrequencyAlarmParams;
import com.yuandian.api.customer_perception.vo.HighFrequencyAlarmVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.service.IHighFrequencyAlarmService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.poi.ExcelTitleUtils;
import com.yuandian.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 说明：高频告警controller
 * @Author yanjun
 * @Date 2021/5/10 8:41
 */
@Slf4j
@RestController
@RequestMapping("/highfrequency")
public class HighFrequencyAlarmController {

    @Autowired
    private IHighFrequencyAlarmService highFrequencyAlarmService;

    /**
     * 查询高频告警列表
     * @param params 高频告警请求对象
     * @return 通用返回对象
     */
    @RequestMapping("/list")
    public R list(@RequestBody HighFrequencyAlarmParams params) {
        params.handleQueryParam();
        PageDataInfo info = highFrequencyAlarmService.queryHighFrequencyAlarmList(params);
        return R.ok(info);
    }


    /**
     * 查询高频告警列表
     * @param params 高频告警请求对象
     * @return 通用返回对象
     */
    @RequestMapping("/listExcel")
    public void listExcel(@RequestBody HighFrequencyAlarmParams params, HttpServletResponse response) throws Exception {
        params.handleQueryParam();

        params.setPageNum(1);
        params.setPageSize(-1);

        PageDataInfo info = highFrequencyAlarmService.queryHighFrequencyAlarmList(params);
        List<HighFrequencyAlarmVo> data = new ArrayList<>();
        try {
            data = (List<HighFrequencyAlarmVo>) info.getData();
        } catch (Exception e) {
            log.error("所选时间表不存在!但表格需要正常导出.");
        }
        int index = 1;
        for (HighFrequencyAlarmVo datum : data) {
            datum.setId(index++);
            datum.setAlarmRate(datum.getAlarmRate()+"%");
            datum.setFailRate(datum.getFailRate()+"%");
        }

        String title = "高频告警数据总览表";


        String fileName = title + System.currentTimeMillis();

        //设置大标题
        List<String> titleParams = new ArrayList<>();
        titleParams.add(title);
        titleParams.add(params.getSystemName() == null?"全系统":params.getSystemName());
        String formTitleName = ExcelTitleUtils.formTitleName(titleParams,params.getStartTime(),params.getEndTime());

        List<List<String>> headList = new ArrayList<>();
        List<String> titleList = Arrays.asList("序号","系统名称", "业务名称","告警量", "告警实值", "告警类型", "统计类型", "访问量",
                "失败率", "健康度", "最近发生时间", "最早发生时间", "结论建议");

        titleList.forEach(s -> {
            List<String> childHeadList = new ArrayList<>();
            childHeadList.add(formTitleName);
            childHeadList.add(s);
            headList.add(childHeadList);
        });

        ExcelUtil.writeExcel(HighFrequencyAlarmVo.class, fileName, title, data, headList, 20, response);
    }



    /**
     * 查询统计类型列表
     * @return 通用返回对象
     */
    @RequestMapping(value = "queryStatTypeList")
    public R queryStatTypeList () {
        return highFrequencyAlarmService.queryStatTypeList();
    }
}
